package com.hd123.rumba.qt.def;

import java.util.ArrayList;
import java.util.List;

import com.hd123.rumba.exception.BusinessException;

public class SQLColumnList extends ArrayList<SQLColumn> implements List<SQLColumn> {
  private final String COLUMNPRIFIX = "A";

  public String getSQLClause() throws BusinessException {
    StringBuffer sb = new StringBuffer();
    boolean isFirst = true;
    for (int i = 0, j = 0; i < size(); ++i) {
      SQLColumn item = get(i);
      String s = item.getSQLSelectExpression();
      if (s != null) {
        if (!isFirst)
          sb.append(", ");
        sb.append(s);
        sb.append(" ");
        sb.append(COLUMNPRIFIX);
        sb.append(++j);
        isFirst = false;
      }
    }
    return sb.toString();
  }

  public boolean hasGAggregation() {
    for (int i = 0; i < size(); ++i) {
      SQLColumn item = get(i);
      if (item.isVisible()) {
        if (item.getGAggregation() != null && item.getGAggregation().length() > 0) {
          return true;
        }
      }
    }
    return false;
  }

  /**
   * 构造适用于结果集的合计语句
   * 
   * @return
   */
  public String getSummaryClause() {
    StringBuffer sb = new StringBuffer();
    boolean isFirst = true;
    for (int i = 0, j = 0; i < size(); ++i) {
      SQLColumn item = get(i);
      if (item.isVisible()) {
        if (!isFirst)
          sb.append(", ");
        if (item.getGAggregation() != null && item.getGAggregation().length() > 0) {
          if ("COUNTDISTINCT".equals(item.getGAggregation().toUpperCase())) {
            sb.append("COUNT(DISTINCT ");
            sb.append(COLUMNPRIFIX);
            sb.append(j + 1);
            sb.append(")");
          } else {
            sb.append(item.getGAggregation());
            sb.append("(");
            sb.append(COLUMNPRIFIX);
            sb.append(j + 1);
            sb.append(")");
          }
        } else
          sb.append("' '");
        sb.append(" ");
        sb.append(COLUMNPRIFIX);
        sb.append(++j);
        isFirst = false;
      }
    }
    return sb.toString();
  }

  public List getVisibleColumnTitles() {
    List list = new ArrayList();
    for (int i = 0; i < size(); ++i) {
      SQLColumn item = (SQLColumn) get(i);
      if (item.isVisible())
        list.add(item.getTitle());
    }
    return list;
  }

  public List getResultTableVisibleColumnNames() {
    List list = new ArrayList();
    String name = null;
    for (int i = 0, j = 0; i < size(); ++i) {
      SQLColumn item = (SQLColumn) get(i);
      if (item.isVisible()) {
        ++j;
        name = COLUMNPRIFIX + j;
        list.add(name);
      }
    }
    return list;
  }

  public String getResultTableColumnNameByColumnTitle(String columnTitle) {
    for (int i = 0, j = 0; i < size(); ++i) {
      SQLColumn item = (SQLColumn) get(i);
      if (item.isVisible()) {
        ++j;
        if (columnTitle.equals(item.getTitle()))
          return COLUMNPRIFIX + j;
      }
    }
    return columnTitle;
  }

  public SQLColumnList getVisibleColumns() {
    SQLColumnList list = new SQLColumnList();
    for (int i = 0; i < size(); ++i)
      if (get(i).isVisible())
        list.add(get(i));
    return list;
  }

  public String debugInfo() {
    StringBuffer sb = new StringBuffer();
    sb.append(getClass().getName() + "\n");
    for (int i = 0; i < size(); ++i) {
      SQLColumn item = (SQLColumn) get(i);
      sb.append(i + ": " + item.debugInfo() + "\n");
    }
    return sb.toString();
  }

  // 查询定义的列转换成查询结果表的列名
  public String columnToResultColumn(String column) {
    int index = 0;
    for (int i = 0; i < size(); i++) {
      SQLColumn item = (SQLColumn) get(i);
      if (item.isVisible()) {
        index++;
        if (column.equalsIgnoreCase(item.getName())) {
          return COLUMNPRIFIX + index;
        }
      }
    }
    return null;
  }

  public List<String> getGroupColumns() {
    List<String> columns = new ArrayList<String>();
    for (int i = 0; i < size(); i++) {
      SQLColumn item = (SQLColumn) get(i);
      if (item.getGroupIndex() >= 0) {
        columns.add(item.getName());
      }
    }
    return columns;
  }
}
